package com.valley.dream.tihai.presenter.impl;

import android.app.Activity;
import android.view.View;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.valley.dream.tihai.R;
import com.valley.dream.tihai.model.dto.InitProblemBO;
import com.valley.dream.tihai.model.dto.SelectProblemByRecommendVO;
import com.valley.dream.tihai.model.entity.Problem;
import com.valley.dream.tihai.presenter.IProblemPresenter;
import com.valley.dream.tihai.service.ProblemService;
import com.valley.dream.tihai.util.network.JsonUtil;
import com.valley.dream.tihai.util.network.RetrofitUtil;
import com.valley.dream.tihai.util.result.Result;
import com.valley.dream.tihai.view.adapter.RecommendRVAdapter;

import java.util.List;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProblemPresenterImpl implements IProblemPresenter {

    ProblemService problemService = RetrofitUtil.getProblemService();

    @Override
    public void createProblem(Activity view, Problem problem) {
        EditText title = view.findViewById(R.id.submit_activity_edit_title);
        EditText score = view.findViewById(R.id.submit_activity_edit_score);
        EditText choiceA = view.findViewById(R.id.submit_activity_edit_choiceA);
        EditText choiceB = view.findViewById(R.id.submit_activity_edit_choiceB);
        EditText choiceC = view.findViewById(R.id.submit_activity_edit_choiceC);
        EditText choiceD = view.findViewById(R.id.submit_activity_edit_choiceD);
        EditText answer = view.findViewById(R.id.submit_activity_edit_answer);
        EditText detail = view.findViewById(R.id.submit_activity_edit_detail);
        problem.setTitle(title.getText().toString());
        problem.setScore(Integer.parseInt(score.getText().toString()));
        problem.setChoiceA(choiceA.getText().toString());
        problem.setChoiceB(choiceB.getText().toString());
        problem.setChoiceC(choiceC.getText().toString());
        problem.setChoiceD(choiceD.getText().toString());
        problem.setAnswer(answer.getText().toString());
        problem.setDetail(detail.getText().toString());
        Call<Result> resultCall = problemService.createProblem(problem);
        resultCall.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                System.out.println(response.body().getMsg());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    @Override
    public Result selectProblemByType(Activity activity, Integer typeId) {

        Call<List<Problem>> listCall = problemService.selectProblemByType(typeId);
        listCall.enqueue(new Callback<List<Problem>>() {
            @Override
            public void onResponse(Call<List<Problem>> call, Response<List<Problem>> response) {

            }

            @Override
            public void onFailure(Call<List<Problem>> call, Throwable t) {

            }
        });

        return Result.ok();
    }

    @Override
    public Result selectProblemByRecommend(Activity activity) {
        Call<List<SelectProblemByRecommendVO>> listCall = problemService.selectProblemByRecommend();
        listCall.enqueue(new Callback<List<SelectProblemByRecommendVO>>() {
            @Override
            public void onResponse(Call<List<SelectProblemByRecommendVO>> call, Response<List<SelectProblemByRecommendVO>> response) {
                RecyclerView recyclerView = activity.findViewById(R.id.recommend_recyclerview);
                List<SelectProblemByRecommendVO> body = response.body();
                RecommendRVAdapter adapter = new RecommendRVAdapter(activity, body);
                recyclerView.setAdapter(adapter);
                LinearLayoutManager manager = new LinearLayoutManager(activity);
                recyclerView.setLayoutManager(manager);
            }

            @Override
            public void onFailure(Call<List<SelectProblemByRecommendVO>> call, Throwable t) {

            }
        });
        return Result.ok();
    }


}
